package com.bjesc_b2c.app.appraiset.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.ClueAdapter;
import com.bjesc_b2c.app.appraiset.bean.WaitTrackBean;
import com.bjesc_b2c.app.appraiset.mvp.presenter.AwaitCluePresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.AwaitCluePresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.AwaitCuleView;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.CreateOrderActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.IntentionOrderDetailsActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.IntentionTouristActivity;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.rxretrofit_build.model.RxBusBean;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.CommonAdapter;
import butterknife.Bind;
import rxbus_base.RxBus;
import rxretrofit_base.model.KeyBean;

/**
 * Created by yidong on 2017/8/8.
 */

public class BaseIntentionFragment extends BaseFragment implements SpringView.OnFreshListener, AwaitCuleView, CommonAdapter.OnItemClickListener,ClueAdapter.createOrderListener{
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.spring_view)
    SpringView springView;
    public AwaitCluePresenter cluePresenter;
    private ClueAdapter clueAdapter;
    public int clueType;
    public int page = 1;
    public int type = 1;
    public String keyWord ="";
    public boolean isLodmore = true;
    public boolean isShowDialog = true;
    private List<WaitTrackBean.ListBean> awaitClueList = new ArrayList<>();
    private Intent mIntent;
    public boolean isPrepared;
    //标志当前页面是否可见
    public boolean isVisible;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_todayplan, null);
        return contentView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        cluePresenter = new AwaitCluePresenterImpl(this);
        //懒加载
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }
    protected void onVisible() {
        lazyLoad();
    }
    protected void lazyLoad() {

    }
    protected void onInvisible() {

    }
    @Override
    public void initView() {
        mIntent = new Intent();
        recyclerView.setBackgroundResource(R.color.bg);
        springView.setListener(this);
        springView.setHeader(new DefaultHeader(mContext));
        springView.setFooter(new DefaultFooter(mContext));
        springView.setType(SpringView.Type.FOLLOW);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
    }
    @Override
    public void initData() {

    }

    public void setClueType(int type) {
        clueType = type;
    }

    @Override
    public void loadStart() {
        if(isShowDialog)
        showLoading();
    }

    @Override
    public void loadCompleted() {
        cancelLoading();
        if (springView != null)
        springView.onFinishFreshAndLoad();
    }

    @Override
    public void loadFailure() {
        cancelLoading();
        if (springView != null)
            springView.onFinishFreshAndLoad();
    }


    @Override
    public void getCuleList(WaitTrackBean bean) {
        RxBus.getInstance().sendData(BaseIntentionFragment.class.getName(), bean.getTotal());
        if (bean != null && bean.getList().size() > 0) {
            refreshData(bean.getList());
            isLodmore = true;
        } else {
            if (page == 1) {
                clueAdapter = null;
                recyclerView.setAdapter(clueAdapter);
            }
            isLodmore = false;
        }
    }


    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        CustomSharedPreferences.saveString(getActivity(), new MapUtil().put(KeyBean.ORDER_ID.valueOf(),clueAdapter.getDatas().get(position).getDispatch_id() ).build(), KeyBean.FILE_NAME.valueOf());
        mIntent.setClass(getActivity(), IntentionOrderDetailsActivity.class);
        mIntent.putExtra("CREATE_ORDER",true);
        startIntent(mIntent);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void createDispatch(String dispatch_id) {
        CustomSharedPreferences.saveString(getActivity(), new MapUtil().put(KeyBean.ORDER_ID.valueOf(), dispatch_id).build(), KeyBean.FILE_NAME.valueOf());
        mIntent.setClass(getActivity(), CreateOrderActivity.class);
        startIntent(mIntent);
    }

    @Override
    public void editTourLevel(String dispatch_id) {

    }

    private void refreshData(List<WaitTrackBean.ListBean> data) {
        if (page == 1) {
            awaitClueList.clear();
        }
        awaitClueList.addAll(data);
        if (clueAdapter == null) {
            clueAdapter = new ClueAdapter(mContext, R.layout.layout_await_task_item, awaitClueList);
            clueAdapter.setOnItemClickListener(this);
            clueAdapter. setShowBtn(true);
            clueAdapter.setOrderListener(this);
            recyclerView.setAdapter(clueAdapter);
        } else {
            clueAdapter.refresh(awaitClueList);
        }
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
         page =1;
        isShowDialog = false;
//        if (clueType == 1) {// 2017.11.15
            cluePresenter.getIntentClueList(mContext, page, type, keyWord,clueType);
//        } else if (clueType == 2) {
//            cluePresenter.getIntentF_ClueList(mContext, page, type, keyWord);
//        }
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadmore() {
        if (isLodmore) {
            page++;
//            if (clueType == 1) {
                cluePresenter.getIntentClueList(mContext, page, type, keyWord,clueType);
//            } else if (clueType == 2) {
//                cluePresenter.getIntentF_ClueList(mContext, page, type, keyWord);
//            }
        }else{
            springView.onFinishFreshAndLoad();
        }
        isShowDialog = false;

    }
    @Override
    protected void doOnNext(RxBusBean rxBusBean) {
        super.doOnNext(rxBusBean);
        if (!isVisible || !isPrepared) {
            return;
        }
        if(rxBusBean.getRequestMark().equals(IntentionTouristActivity.class.getName())) {
            if (rxBusBean.getO() instanceof Intent) {
                isShowDialog = true;
                page = 1;
                type = ((Intent) rxBusBean.getO()).getIntExtra("type",1);
                keyWord = ((Intent) rxBusBean.getO()).getStringExtra("keyWord");
//                if (clueType == 1) {
                    cluePresenter.getIntentClueList(mContext, page, type, keyWord,clueType);
//                } else if (clueType == 2) {
//                    cluePresenter.getIntentF_ClueList(mContext, page, type, keyWord);
//                }
            }
        }else if(rxBusBean.getRequestMark().equals("keyWord")){
            keyWord = rxBusBean.getO().toString();
        }
    }
}
